package com.kd.juhaowan.ui.fragment;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;


import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallbackProxy;
import com.kd.juhaowan.R;
import com.kd.juhaowan.http.api.GameListDateApi;
import com.kd.juhaowan.http.model.CateModel;
import com.kd.juhaowan.http.model.DateModel;
import com.kd.juhaowan.http.model.GameModel;
import com.kd.juhaowan.http.model.HttpData;
import com.kd.juhaowan.http.model.HttpListData;
import com.kd.juhaowan.ui.GameDetailActivity;
import com.kd.juhaowan.ui.GameDetailActivity2;
import com.kd.juhaowan.ui.adapter.BaseRecyclerViewAdapter;
import com.kd.juhaowan.ui.holder.GameCateViewHolder;
import com.kd.juhaowan.ui.holder.GameDayViewHolder;
import com.kd.juhaowan.ui.holder.ServerGameViewHolder;
import com.kd.juhaowan.ui.listener.OnItemClickListener;
import com.kd.juhaowan.utils.LogUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import butterknife.BindView;

public class GameServerFragment extends SmartFreshBaseFragment implements OnItemClickListener {

    @BindView(R.id.left_listview)
    RecyclerView leftRecyclerView;
    @BindView(R.id.right_listview)
    RecyclerView rightRecyclerView;
    @BindView(R.id.top_listview)
    RecyclerView topRecyclerView;

    @BindView(R.id.view_refreshlist_refresh)
    SmartRefreshLayout refreshLayout;

    private List<CateModel> models;
    private List<DateModel> dateModels;
    @Override
    protected int getViewLayoutResourceId() {
        return R.layout.fragment_game_server;
    }

    private String catid;

    @Override
    protected void onButterKnifeCreateViewAfter() {
        super.onButterKnifeCreateViewAfter();
        models = manager.getCateList();
        if(models.size()>0){
            catid = models.get(0).getId();
        }
        getDate();
        setAdapter();
        queryDate(1);
    }

    @Override
    protected void reInitDate() {
        queryDate(1);
    }

    private void getDate(){
        dateModels = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String currentDate = dateFormat.format(calendar.getTime());
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        String yesterDate = dateFormat.format(calendar.getTime());
        calendar.add(Calendar.DAY_OF_MONTH, 2);
        String tomorrowDate = dateFormat.format(calendar.getTime());
        LogUtil.debugLog(yesterDate+"\n"+currentDate+"\n"+tomorrowDate);
        DateModel yes=new DateModel();
        yes.setId(yesterDate);
        yes.setName("昨天");
        DateModel today=new DateModel();
        today.setId(currentDate);
        today.setName("今天");
        DateModel tomorry=new DateModel();
        tomorry.setId(tomorrowDate);
        tomorry.setName("明天");
        dateModels.add(yes);
        dateModels.add(today);
        dateModels.add(tomorry);
    }

    private void queryDate(int page){
        EasyHttp.post(this).api(new GameListDateApi(catid,page,dayAdapter.getItem(dayIndex).getId())).request(new HttpCallbackProxy<HttpListData<GameModel>>(this){
            @Override
            public void onHttpSuccess(HttpListData<GameModel> result, boolean cache) {
                super.onHttpSuccess(result, cache);
//                        LogUtil.debugLog(result.getData().getAccess_token());
//                        Toast.makeText(AccountLoginActivity.this,"登录成功",Toast.LENGTH_SHORT).show();
//                bannerModels = result.getData();
                LogUtil.debugLog("gameTagFragment",page+".."+(result.getData().getList()==null?0:result.getData().getList().size()));
                if(page==1){
//                    List<GameModel> lsit =new ArrayList<>();
//                    List<GameModel> temp = result.getData().getList();
//                    if(temp!=null){
//                        lsit.addAll(temp);
//                        lsit.addAll(temp);
//                        lsit.addAll(temp);
//                        lsit.addAll(temp);
//                        lsit.addAll(temp);
//                        lsit.addAll(temp);
//                        lsit.addAll(temp);
//                        lsit.addAll(temp);
//                        lsit.addAll(temp);
//                        lsit.addAll(temp);
//                        lsit.addAll(temp);
//                        lsit.addAll(temp);
//                        lsit.addAll(temp);
//                        lsit.addAll(temp);
//                    }
                    gameAdapter.resetAdapterData(result.getData().getList());
                }else {

                    gameAdapter.appendAdapterData(result.getData().getList());
                }
//                finishRefreshOrLoadMore(true,false);
            }
        });
    }


    @Override
    protected SmartRefreshLayout getSmartRefreshLayout() {
        return refreshLayout;
    }

    @Override
    protected String getSmartRefreshTaskTag() {
        return null;
    }

    @Override
    protected void onRequestNextPageData(int page) {
        queryDate(page);
    }

    @Override
    protected boolean isNullData(HttpData response) {
        return false;
    }

    private BaseRecyclerViewAdapter<GameCateViewHolder, CateModel> cateAdapter;
    private BaseRecyclerViewAdapter<GameDayViewHolder, DateModel> dayAdapter;
    private BaseRecyclerViewAdapter<ServerGameViewHolder, GameModel> gameAdapter;
    private int selectIndex = 0;
    private int dayIndex = 1;
    private void setAdapter(){
        dayAdapter = new BaseRecyclerViewAdapter<GameDayViewHolder, DateModel>() {
            @NonNull
            @Override
            public GameDayViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                LayoutInflater inflater = LayoutInflater.from(parent.getContext());
                View view = inflater.inflate(R.layout.item_game_date, parent, false);
                GameDayViewHolder holder=new GameDayViewHolder(view);
                holder.setOnItemClickListener(getOnItemClickListener());
                return  holder;

            }

            @Override
            public void onBindViewHolder(@NonNull GameDayViewHolder holder, int position) {
                holder.setDate(getItem(position),dayIndex==position);

            }
        };
        dayAdapter.setOnItemClickListener(this);
        dayAdapter.resetAdapterData(dateModels);
        LinearLayoutManager layoutManager3 = new LinearLayoutManager(getContext());
        layoutManager3.setOrientation(RecyclerView.HORIZONTAL);

        topRecyclerView.setLayoutManager(layoutManager3);
        topRecyclerView.setAdapter(dayAdapter);





        cateAdapter = new BaseRecyclerViewAdapter<GameCateViewHolder, CateModel>() {
            @NonNull
            @Override
            public GameCateViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                LayoutInflater inflater = LayoutInflater.from(parent.getContext());
                View view = inflater.inflate(R.layout.item_game_cate, parent, false);
                GameCateViewHolder holder=new GameCateViewHolder(view);
                holder.setOnItemClickListener(getOnItemClickListener());
                return  holder;

            }

            @Override
            public void onBindViewHolder(@NonNull GameCateViewHolder holder, int position) {
                holder.setDate(getItem(position),selectIndex==position);

            }
        };
        cateAdapter.setOnItemClickListener(this);
        cateAdapter.resetAdapterData(models);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        layoutManager.setOrientation(RecyclerView.VERTICAL);

        leftRecyclerView.setLayoutManager(layoutManager);
        leftRecyclerView.setAdapter(cateAdapter);


        gameAdapter = new BaseRecyclerViewAdapter<ServerGameViewHolder, GameModel>() {
            @NonNull
            @Override
            public ServerGameViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                LayoutInflater inflater = LayoutInflater.from(parent.getContext());
                View view = inflater.inflate(R.layout.item_server_game, parent, false);
                ServerGameViewHolder holder=new ServerGameViewHolder(view);
                holder.setOnItemClickListener(getOnItemClickListener());
                return  holder;

            }

            @Override
            public void onBindViewHolder(@NonNull ServerGameViewHolder holder, int position) {
                holder.setDate(getItem(position),position);

            }
        };
        gameAdapter.setOnItemClickListener(this);
        gameAdapter.resetAdapterData(null);
        LinearLayoutManager layoutManager2 = new LinearLayoutManager(getContext());
        layoutManager2.setOrientation(RecyclerView.VERTICAL);

        rightRecyclerView.setLayoutManager(layoutManager2);
        rightRecyclerView.setAdapter(gameAdapter);


    }

    @Override
    public void onItemClick(RecyclerView.ViewHolder viewHolder, View view, int position) {
        if(viewHolder instanceof GameCateViewHolder){
            selectIndex = position;
            cateAdapter.notifyDataSetChanged();
            catid=cateAdapter.getItem(position).getId();
            queryDate(1);
        }else if (viewHolder instanceof GameDayViewHolder){
            dayIndex = position;
            dayAdapter.notifyDataSetChanged();
            queryDate(1);
        }else if(viewHolder instanceof ServerGameViewHolder){
            Intent intent= new Intent(getActivity(), GameDetailActivity.class);
            GameModel model =gameAdapter.getItem(position);
            intent.putExtra("id",model.getGame_id());
            startActivity(intent);
        }
    }
}
